﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;


namespace GWLauncher
{
    public partial class GWLauncher : Form
    {
        public GWLauncher()
        {
            InitializeComponent();
        }

        private void GWLauncher_Load(object sender, EventArgs e)
        {
            String szGWPath;
            String szLogPath;
            StreamReader sr;
            String strAccount = "";
            String strPassword = "";
            String strAccountCipher = "";
            String strPasswordCipher = "";
            String strRecon = "";
            int numIntValue;
            String strCipherKey = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";


            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ArenaNet\Guild Wars");
            szGWPath = (String)rk.GetValue("Path");

            txtGWPath.Text = szGWPath;

            szLogPath = szGWPath.Substring(0, szGWPath.LastIndexOf(@"\")+1);
            szLogPath += @"NetworkDiag.log";

            if (File.Exists(szLogPath))
                btnDiagsReport.Enabled = true;

            cmbSound.SelectedIndex = 0;
            cmbModes.SelectedIndex = 0;

            if (File.Exists(Environment.GetEnvironmentVariable("HOMEPATH") + "\\GWLUser.dat"))
            {
                cbxAutoLogon.Checked = true;
                sr = File.OpenText(Environment.GetEnvironmentVariable("HOMEPATH") + "\\GWLUser.dat");

                strAccountCipher = sr.ReadLine();

                for (int i = 0; i < strAccountCipher.Length; i++)
                {
                    if (strAccountCipher[i].CompareTo(' ') != 0)
                    {
                        strRecon += strAccountCipher[i];
                    }
                    else if (strAccountCipher[i].CompareTo(' ') == 0 && strRecon != "")
                    {
                        numIntValue = Convert.ToInt32(strRecon);
                        numIntValue = numIntValue / (int)strCipherKey[strAccount.Length];
                        numIntValue = numIntValue >> 4;
                        strAccount += (char)numIntValue;
                        strRecon = "";
                    }
                }

                strPasswordCipher = sr.ReadLine();

                for (int i = 0; i < strPasswordCipher.Length; i++)
                {
                    if (strPasswordCipher[i].CompareTo(' ') != 0)
                    {
                        strRecon += strPasswordCipher[i];
                    }
                    else if (strPasswordCipher[i].CompareTo(' ') == 0 && strRecon != "")
                    {
                        numIntValue = Convert.ToInt32(strRecon);
                        numIntValue = numIntValue / (int)strCipherKey[strPassword.Length];
                        numIntValue = numIntValue >> 4;
                        strPassword += (char)numIntValue;
                        strRecon = "";
                    }
                }

                txtAccount.Text = strAccount;
                txtPassword.Text = strPassword;

            }
        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo(txtGWPath.Text);

            switch (cmbModes.SelectedIndex)
            {
                case 0:
                    if (cbxAutoLogon.Checked == true)
                        psi.Arguments += (@"-email " + txtAccount.Text + " -password " + txtPassword.Text);

                    if (cmbSound.SelectedIndex == 1)
                    {
                        if (rbnNewSound.Checked == true)
                            psi.Arguments += @" -sndwinmm";
                        else if (rbnASIO.Checked == true)
                            psi.Arguments += @" -sndasio";
                        else if (rbnDXSound.Checked == true)
                            psi.Arguments += @" -dsound";
                    }
                    else if (cmbSound.SelectedIndex == 2)
                        psi.Arguments += @" -nosound";

                    if (cbxBitmaps.Checked == true)
                        psi.Arguments += @" -bmp";

                    if (cbxPerf.Checked == true)
                        psi.Arguments += @" -perf";

                    if (cbxDirectX8.Checked == true)
                        psi.Arguments += @" -dx8";

                    if (cbxNoShaders.Checked == true)
                        psi.Arguments += @" -noshaders";
                    break;
                case 1:
                    psi.Arguments = @"-image";
                    break;
                case 2:
                    psi.Arguments = @"-diag";
                    btnDiagsReport.Enabled = true;
                    break;
                case 3:
                    psi.Arguments = @" -repair";
                    break;
            }                

            Process.Start(psi);
        }

        private void cbxAutoLogon_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxAutoLogon.Checked == true)
            {
                txtAccount.Enabled = true;
                txtPassword.Enabled = true;
                txtAccount.BackColor = Color.White;
                txtPassword.BackColor = Color.White;
                btnSaveCred.Enabled = true;
            }
            else
            {
                txtAccount.Enabled = false;
                txtPassword.Enabled = false;
                txtAccount.BackColor = Color.DarkGray;
                txtPassword.BackColor = Color.DarkGray;
                btnSaveCred.Enabled = false;
            }
        }

        private void btnDiagsReport_Click(object sender, EventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo();

            if (File.Exists(@"C:\Windows\Notepad.exe"))
            {
                psi.FileName = @"C:\Windows\Notepad.exe";
                
                String szGWPath;
                String szLogPath;

                RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ArenaNet\Guild Wars");
                szGWPath = (String)rk.GetValue("Path");

                szLogPath = szGWPath.Substring(0, szGWPath.LastIndexOf(@"\")+1);
                szLogPath += @"NetworkDiag.log";

                psi.Arguments = szLogPath;
                if (File.Exists(szLogPath))
                    Process.Start(psi);
                else
                    MessageBox.Show(@"Diagnostics Log File Removed.  Bypassing...");
            }
            else if (File.Exists(@"C:\WINNT\Notepad.exe"))
            {
                psi.FileName = @"C:\WINNT\Notepad.exe";

                String szGWPath;
                String szLogPath;

                RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ArenaNet\Guild Wars");
                szGWPath = (String)rk.GetValue("Path");

                szLogPath = szGWPath.Substring(0, szGWPath.LastIndexOf(@"\") + 1);
                szLogPath += @"NetworkDiag.log";

                psi.Arguments = szLogPath;
                if (File.Exists(szLogPath))
                    Process.Start(psi);
                else
                    MessageBox.Show(@"Diagnostics Log File Removed.  Bypassing...");
            }
            else
                MessageBox.Show(@"Unable to locate notepad text editor.  Exiting...");
        }

        private void cmbSound_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSound.SelectedIndex == 0)
            {
                rbnASIO.Enabled = false;
                rbnDXSound.Enabled = false;
                rbnNewSound.Enabled = false;
                rbnNewSound.Checked = false;
                rbnDXSound.Checked = false;
                rbnASIO.Checked = false;
            }
            else if (cmbSound.SelectedIndex == 1)
            {
                rbnASIO.Enabled = true;
                rbnDXSound.Enabled = true;
                rbnNewSound.Enabled = true;
                rbnNewSound.Checked = true;
            }
            else if (cmbSound.SelectedIndex == 2)
            {
                rbnASIO.Enabled = false;
                rbnDXSound.Enabled = false;
                rbnNewSound.Enabled = false;
                rbnNewSound.Checked = false;
                rbnDXSound.Checked = false;
                rbnASIO.Checked = false;
            }
        }

        private void btnSaveCred_Click(object sender, EventArgs e)
        {
            if (txtAccount.TextLength > 0 && txtPassword.TextLength > 0)
            {
                // Encrypt and save the login information

                String strAccount = "";
                String strPassword = "";
                String strAccountCipher = "";
                String strPasswordCipher = "";
                String strCipherKey = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
                int numIntValue;
                StreamWriter sw;

                strAccount = txtAccount.Text;
                strPassword = txtPassword.Text;

                for (int i = 0; i < strAccount.Length; i++)
                {
                    numIntValue = ((int)strAccount[i] << 4) * strCipherKey[i];
                    strAccountCipher += numIntValue.ToString() + " ";
                    numIntValue = 0;
                }
                strAccountCipher.Trim();

                for (int i = 0; i < strPassword.Length; i++)
                {
                    numIntValue = ((int)strPassword[i] << 4) * strCipherKey[i];
                    strPasswordCipher += numIntValue.ToString() + " ";
                    numIntValue = 0;
                }
                strPasswordCipher.Trim();

                if (File.Exists("%HOMEPATH\\GWLUser.dat"))
                {
                    File.Delete("%HOMEPATH\\GWLUser.dat");

                    sw = File.CreateText(Environment.GetEnvironmentVariable("HOMEPATH") + "\\GWLUser.dat");
                    sw.WriteLine(strAccountCipher);
                    sw.WriteLine(strPasswordCipher);
                    sw.Close();
                }
                else
                {
                    sw = File.CreateText(Environment.GetEnvironmentVariable("HOMEPATH") + "\\GWLUser.dat");
                    sw.WriteLine(strAccountCipher);
                    sw.WriteLine(strPasswordCipher);
                    sw.Close();
                }


            }
            else
            {
                MessageBox.Show(@"Missing either the Account name or the Password.  Please ensure that both are filled.");
            }
        }

        private void btnFileOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = txtGWPath.Text;
            ofd.Filter = "Guild Wars Executable (gw.exe)|gw.exe|All files (*.*)|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)
                txtGWPath.Text = ofd.FileName;

            
        }

    }
}
